package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentTejiDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentTeji;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTejiMapper;
import cn.edu.sgu.www.mhxysy.pager.equipment.EquipmentTejiPager;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentTejiService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentTejiServiceImpl implements EquipmentTejiService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final EquipmentTejiMapper equipmentTejiMapper;

	@Autowired
	public EquipmentTejiServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			EquipmentTejiMapper equipmentTejiMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.equipmentTejiMapper = equipmentTejiMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_TETJ;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(EquipmentTejiDTO equipmentTejiDTO) {
		EquipmentTeji equipmentTeji = toEntity(equipmentTejiDTO);

		equipmentTeji.setId(null);

		equipmentTejiMapper.insert(equipmentTeji);
	}

	@Override
	public void updateById(EquipmentTejiDTO equipmentTejiDTO) {
		ObjectUtils.requireNonNull(equipmentTejiDTO.getId(), CustomMessages.ID_NOT_NULL);

		EquipmentTeji equipmentTeji = toEntity(equipmentTejiDTO);

		equipmentTejiMapper.updateById(equipmentTeji);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<EquipmentTeji> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				equipmentTejiMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<EquipmentTeji> selectAll() {
		QueryWrapper<EquipmentTeji> queryWrapper = new QueryWrapper<>();

		queryWrapper.orderByAsc("type");

		return equipmentTejiMapper.selectList(queryWrapper);
	}

	@Override
	public EquipmentTeji selectById(Integer id) {
		return equipmentTejiMapper.selectById(id);
	}

	@Override
	public Page<EquipmentTeji> selectByPage(EquipmentTejiPager pager) {
		QueryWrapper<EquipmentTeji> queryWrapper = new QueryWrapper<>();
		Page<EquipmentTeji> page = Pager.ofPage(pager);

		queryWrapper.eq(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);
		queryWrapper.eq(
				pager.getType() != null,
				"type", pager.getType()
		);

		// 得到order by语句
		String statement = Pager.getOrderByStatement(pager);

		queryWrapper.last(statement);

		return equipmentTejiMapper.selectPage(page, queryWrapper);
	}

	/**
	 * DTO转数据库实体类
	 * @param equipmentTejiDTO EquipmentTejiDTO对象
	 * @return 根据EquipmentTejiDTO对象封装得到的EquipmentTeji对象
	 */
	private EquipmentTeji toEntity(EquipmentTejiDTO equipmentTejiDTO) {
		EquipmentTeji equipmentTeji = new EquipmentTeji();

		equipmentTeji.setId(equipmentTejiDTO.getId());
		equipmentTeji.setName(equipmentTejiDTO.getName());
		equipmentTeji.setType(equipmentTejiDTO.getType());
		equipmentTeji.setNote(equipmentTejiDTO.getNote());
		equipmentTeji.setFennu(equipmentTejiDTO.getFennu());
		equipmentTeji.setScore(equipmentTejiDTO.getScore());

		return equipmentTeji;
	}

}